package com.aaa.test1.servlet;


import com.aaa.test1.dao.StudentDAO;
import com.aaa.test1.dao.impl.StudentDAOImpl;
import com.aaa.test1.entity.Student;
import com.aaa.test1.result.AjaxResult;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/stu/*")
public class StudentServlet extends HttpServlet {
    private StudentDAO studentDAO = new StudentDAOImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/json;charset=UTF-8");
        String requestURI = req.getRequestURI();
        String[] split = requestURI.split("/");
        String path = split[split.length - 1];

        if ("select".equals(path)) {
            select(req, resp);
        } else if ("delStu".equals(path)) {
            delStu(req, resp);
        } else if ("addStu".equals(path)) {
            addStu(req, resp);
        }else if("getStu".equals(path)){
            getStu(req,resp);
        }else if("update".equals(path)){
            update(req,resp);

        }
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Map<String, Object>> students = studentDAO.Select();
        AjaxResult ajaxResult = new AjaxResult(20002, "获取成功", students);
        String s = JSON.toJSONString(ajaxResult);
        resp.getWriter().write(s);


    }

    protected void delStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int i = studentDAO.delStudent(id);
        System.out.println(i);
        AjaxResult ajaxResult = new AjaxResult(20001, "删除成功", i);
        String s = JSON.toJSONString(ajaxResult);
        resp.getWriter().write(s);

    }

    protected void addStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String address = req.getParameter("address");
        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), address);
        int i = studentDAO.addStudent(student);
        AjaxResult ajaxResult = new AjaxResult(20003, "添加成功", i);
        String s = JSON.toJSONString(ajaxResult);
        resp.getWriter().write(s);

    }

    protected void getStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id  =req.getParameter("id");
        Map<String,Object> stu = studentDAO.getStu(id);
        AjaxResult ajaxResult = new AjaxResult(20004, "获取成功", stu);
        String s = JSON.toJSONString(ajaxResult);
        resp.getWriter().write(s);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name=req.getParameter("name");
        String age =req.getParameter("age");
        String address =req.getParameter("address");
        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), address);
        int i = studentDAO.updateStudent(student);
        AjaxResult ajaxResult = new AjaxResult(20005, "修改成功", i);
        String s = JSON.toJSONString(ajaxResult);
        resp.getWriter().write(s);
}
}